// index.js
// 获取应用实例
const app = getApp()
var baseurl = app.baseurl.url;
Page({
  data: {
    informat: false,
    currentIndex: 0,
    disable: false,
    flag: true,
    checked: '',
    is_agree: false,
    acceptRule: false,
    banner: '',
    news: '',
    order: '', //预约
    notice: '', //预约须知
    prizeid: '',
    isover: '',
    text: "滚动字幕~~~~",
    step: 1, //滚动速度
    distance: 200, //初始滚动距离
    space: 10,
    interval: 30 // 时间间隔
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {

    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }

    // 轮播
    wx.request({
      url: `${baseurl}api/banner/banner`,
      method: 'POST',
      data: {},
      success: (res) => {
        this.setData({
          banner: res.data.data
        })
      }
    })
    // 须知
    wx.request({
      url: `${baseurl}api/system/mustKnow`,
      method: 'POST',
      data: {},
      success: (res) => {
        // console.log(res.data.data);
        this.setData({
          notice: res.data.data
        })
      }
    })
    if (this.data.informat == true) { // 如果缓存数组存在数据
      this.setData({
        informat: true,
        // textareaInfo: textareaInfo,
      })
      // 预约抢购
      wx.request({
        url: `${baseurl}api/Prize/prizeList`,
        method: 'POST',
        data: {},
        success: (res) => {
          let list = []
          list = res.data.data
          // list.slice(0, 3)
          if (list.length >= 3) {
            this.setData({
              order: list.slice(0, 3)
            })
          } else {
            this.setData({
              order: list
            })
            // console.log(this.data.order);
          }

        }
      })
      console.log('不重复');
    } else {
      console.log('重复运行');

    }


    // 公司快讯
    wx.request({
      url: `${baseurl}api/news/news`,
      method: 'POST',
      data: {},
      success: (res) => {
        let list = []
        list = res.data.data
        if (list >= 4) {
          this.setData({
            news: list.slice(0, 4)
          })
        } else {
          this.setData({
            news: list
          })
        }
      }
    })

    // 公告
    wx.request({
      url: `${baseurl}api/news/notice`,
      method: 'POST',
      data: {},
      success: (res) => {
        this.setData({
          text: res.data.data
        })
        // console.log(res.data.data);
      }
    })
  },
  /* 这里实现轮播控制中间凸显图片的样式 */
  handleChange: function (e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },

  // 点击头条
  click() {
    wx.navigateTo({
      url: '/pages/notice/notice'
    })
  },

  // 立即预约
  now_order(id) {
    // console.log(id.currentTarget.dataset.cap);
    // 保存商品id
    wx.setStorageSync('prizeid', id.currentTarget.dataset.cap)
    // console.log(wx.getStorageSync("prizeid"));
    this.setData({
      flag: false,
      prizeid: id.currentTarget.dataset.cap
    })
  },
  // 复选框是否勾选
  checkchange(e) {
    // console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    const checked = e.detail.value
    // console.log(checked);
    this.setData({
      checked
    })
  },

  // 预约须知确定按钮
  sure() {
    // this.setData({
    //   flag: true
    // }),
    var that = this
    let openid = wx.getStorageSync("openid")
    // console.log(wx.getStorageSync("openid"));
    // 是否有openid登录
    if (openid != '') {
      this.setData({
        flag: true
      })
      // console.log(this.data.checked == '');
      if (this.data.checked == '') {
        // console.log('未选中');
        wx.showToast({
          title: '请阅读并同意预约须知',
          icon: 'none',
          duration: 2000
        })
      } else {
        // console.log('已选中');
        wx.request({
          url: `${baseurl}api/Login/userinfo`,
          method: 'POST',
          data: {
            id: wx.getStorageSync("id"),
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            console.log(res.data.data.tel);
            let onid = that.data.prizeid
            // console.log(onid);
            if (res.data.data.tel == null) {
              // console.log(onid);
              // console.log(res.data.data.tel);
              wx.navigateTo({
                url: `/pages/message/message`
              })
            } else {
              wx.request({
                url: `${baseurl}api/Reservation/reserinfo`,
                method: 'POST',
                data: {
                  openid: wx.getStorageSync("openid"),
                  user_id: wx.getStorageSync("id"),
                  prizeid: that.data.prizeid
                },
                success(res) {
                  that.setData({
                    disable: true
                  })
                  console.log(res.data.msg);
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                  })
                  setTimeout(
                    function () {
                      wx.reLaunch({
                        url: '/pages/order/order',
                      })

                    },
                    2000)

                }

              })



            }
          }
        })

      }

      // console.log(111);
    } else {

      wx.reLaunch({
        url: '/pages/login/login'
      })


      // console.log(222);

    }


  },
  onbox() {
    this.setData({
      flag: true
    })
  },
  catchtap() {
    this.setData({
      flag: false
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 滚动字幕
  onShow: function () {
    var that = this;

    var query = wx.createSelectorQuery();
    //选择id
    query.select('#mjltest').boundingClientRect()
    query.exec(function (res) {
      var length = res[0].width;
      var windowWidth = wx.getSystemInfoSync().windowWidth; // 屏幕宽度

      that.setData({
        length: length,
        windowWidth: windowWidth,
        space: windowWidth
      });
      that.scrollling(); // 第一个字消失后立即从右边出现
    });
    this.onLoad()
  },
  scrollling: function () {
    var that = this;
    var length = that.data.length; //滚动文字的宽度
    var windowWidth = that.data.windowWidth; //屏幕宽度
    var interval = setInterval(function () {
      var maxscrollwidth = length + that.data.space;
      var left = that.data.distance;
      // console.log(maxscrollwidth);
      // console.log(left);
      if (left < maxscrollwidth) { //判断是否滚动到最大宽度
        that.setData({
          distance: left + that.data.step
        })
      } else {
        that.setData({
          distance: 0 // 直接重新滚动
        });
        clearInterval(interval);
        that.scrollling();
      }
    }, that.data.interval);
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onShow: function () {
    console.log(this.data.informat);
    this.setData({
      // 初始化date
      informat: true,
      winWidth: 0,
      winHeight: 0,
      currentTab: 0,
      reservStatus: [],
      title: '',
      order: [],  // 列表
      total: '', // 总共条数
      per_page: 4, // 每页条数
      last_page: 1, // 最后一页
      current_page: 1,// 当前页
      isloading: false
    })
    this.onLoad()
    console.log(this.data.informat);
  },
})


